home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wil4c10.zip / VIEW.C < prev    next >
C/C++ Source or Header  |  1997-07-26  |  9KB  |  346 lines

  1. /*
  2. **  VIEW.C
  3. **
  4. **  WWW client. Uses synchronous calls.
  5. */
  6.  
  7. #include <windows.h>
  8. #include <winsock.h>
  9.  
  10. #include "wil.h"
  11. #include "message.h"
  12. #include "paint.h"
  13. #include "about.h"
  14. #include "str.h"
  15.  
  16. #ifdef WIN32
  17. #define USE_INS HINSTANCE
  18. #define USE_PTR PSTR
  19. #else
  20. #define USE_INS HANDLE
  21. #define USE_PTR LPSTR
  22. #endif
  23.  
  24. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  25.  
  26. /* globals */
  27.  
  28. HWND hMainWnd;            /* main window handle */
  29.  
  30. #define BS            8
  31. #define LF           10
  32. #define CR           13
  33. #define MAX_BUF     128
  34. #define MAX_STR      40
  35.  
  36. #define ONE_SEC     1000
  37. #define TEN_SEC    10000
  38. #define TWENTY_SEC 20000
  39.  
  40. #define WWW_PORT  80
  41.  
  42. static HMENU hMenu;
  43. static USE_INS hInstance;
  44. static int WinWidth = 8 * NCOLS;
  45. static int WinHeight = 12 * NROWS + 48;
  46. static char Temp[MAX_BUF+8];
  47. static int  InBufLen = 0;
  48. static char InBuffer[MAX_BUF+1];
  49. static SOCKET Socket = 0;
  50. static ULONG  HostAddr = 0;
  51. static LPSTR  Ptr;
  52. static char WorkBuf[MAX_BUF+1] = "";
  53. static char URL[MAX_STR] = "";
  54. static char URI[MAX_STR] = "";
  55. static HCURSOR ArrowCursor;
  56. static HCURSOR WaitCursor;
  57.  
  58. /* sets the focus to client area */
  59.  
  60. static void SetTheFocus(void)
  61. {/* create client area caret */
  62.  CreateCaret(hMainWnd,NULL,3,10);
  63.  SetCaretPos(PaintGetColPos(),PaintGetRowPos());
  64.  ShowCaret(hMainWnd);
  65. }
  66.  
  67. /* append character to buffer */
  68.  
  69. static void Add2Buffer(char Chr)
  70. {/* add char to input buffer */
  71.  switch(Chr)
  72.    {case BS:
  73.       if(InBufLen>0)
  74.         {/* backup on screen */
  75.          DisplayChar(BS);
  76.          /* remove last char from buffer */
  77.          InBufLen--;
  78.         }
  79.       break;
  80.     default:
  81.       /* display char */
  82.       DisplayChar(Chr);
  83.       /* put into buffer */
  84.       if(InBufLen<MAX_BUF) InBuffer[InBufLen++] = Chr;
  85.       break;
  86.    }
  87. }
  88.  
  89. /* display error code & text */
  90.  
  91. static void DisplayError(int Code, LPSTR Msg)
  92. {DisplayString("ERROR: ");
  93.  if(Msg) DisplayString(Msg);
  94.  if(Code)
  95.    {wilErrorText(Code,(LPSTR)Temp,50);
  96.     DisplayLine((LPSTR)Temp);
  97.    }
  98.  SetCursor(ArrowCursor);
  99. }
  100.  
  101. /* WinMain */
  102.  
  103. #ifdef WIN32
  104. int WINAPI
  105. #else
  106. int PASCAL
  107. #endif
  108. WinMain(USE_INS hInst, USE_INS hPrevInstance,
  109.         USE_PTR szCmdLine,  int nCmdShow)
  110. {WNDCLASS  wc;
  111.  MSG msg;
  112.  BOOL Result;
  113.  if(!hPrevInstance)
  114.    {/* register main window class */
  115.     wc.style = CS_HREDRAW | CS_VREDRAW;
  116.     wc.lpfnWndProc = MainWndProc;
  117.     wc.cbClsExtra = 0;
  118.     wc.cbWndExtra = 0;
  119.     wc.hInstance = hInst;
  120.     wc.hIcon = LoadIcon(hInst, "HostIcon");
  121.     wc.hCursor = NULL;
  122.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  123.     wc.lpszMenuName =  "HostMenu";
  124.     wc.lpszClassName = "HostWClass";
  125.     Result = RegisterClass(&wc);
  126.     if(!Result) return FALSE;
  127.    }
  128.  
  129.  /* create main window */
  130.  hInstance = hInst;
  131.  hMainWnd = CreateWindow(
  132.         "HostWClass",   "WWW_VIEW",    WS_OVERLAPPEDWINDOW,
  133.         CW_USEDEFAULT,  CW_USEDEFAULT,
  134.         WinWidth,       WinHeight,
  135.         NULL,           NULL,
  136.         hInstance,      NULL);
  137.  ShowWindow(hMainWnd, nCmdShow);
  138.  UpdateWindow(hMainWnd);
  139.  hMenu = GetMenu(hMainWnd);
  140.  
  141.  /* window control loop */
  142.  
  143.  while(GetMessage(&msg,NULL,0,0))
  144.    {
  145.     TranslateMessage(&msg);
  146.     DispatchMessage(&msg);
  147.    }
  148.  return (msg.wParam);
  149. } /* end WinMain */
  150.  
  151. #ifdef WIN32
  152. LRESULT CALLBACK
  153. #else
  154. long FAR PASCAL
  155. #endif
  156. MainWndProc(HWND hWindow,UINT iMsg,WPARAM wParam,LPARAM lParam)
  157. {int Code;
  158.  HDC hDC;
  159.  PAINTSTRUCT ps;
  160. #ifdef WIN32
  161. #else
  162.  static FARPROC lpfnAboutDlgProc;
  163. #endif
  164.  hMainWnd = hWindow;
  165.  switch (iMsg)
  166.     {case WM_CREATE:
  167.       /* create cursors */
  168.       ArrowCursor = LoadCursor(NULL, IDC_ARROW);
  169.       WaitCursor = LoadCursor(NULL, IDC_WAIT);
  170.       SetCursor(ArrowCursor);
  171. #ifdef WIN32
  172. #else
  173.        /* create thunk for Win16 */
  174.        lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc,hInstance);
  175. #endif
  176.       /* initialize paint module */
  177.       PaintInit();
  178.       /* attach WINSOCK */
  179.       DisplayString("Attaching WINSOCK...");
  180.       Code = wilAttach();
  181.       DisplayLine("OK");
  182.       if(Code<0) DisplayError(Code,"wilAttach fails:");
  183.       else
  184.         {wsprintf((LPSTR)Temp," Description: %s", wilGetDescription() );
  185.          DisplayLine((LPSTR)Temp);
  186.          wsprintf((LPSTR)Temp," My HostName: %s", wilGetMyHostName() );
  187.          DisplayLine((LPSTR)Temp);
  188.          wsprintf((LPSTR)Temp," My HostAddr: %s", wilGetMyHostDotted(0) );
  189.          DisplayLine((LPSTR)Temp);
  190.         }
  191.       break;
  192.  
  193.      case WM_COMMAND:
  194.          switch(wParam)
  195.            {
  196.             case MSG_ABOUT:
  197. #ifdef WIN32
  198.                DialogBox(hInstance, "AboutBox", hMainWnd, AboutDlgProc);
  199. #else
  200.                DialogBox(hInstance, "AboutBox", hMainWnd, lpfnAboutDlgProc);
  201. #endif
  202.                return 0;
  203.             case MSG_EXIT:
  204.               wilRelease();
  205.               DestroyWindow(hMainWnd);
  206.               break;
  207.  
  208.             case MSG_DEBUG:
  209.               Code = wilDebug(0);
  210.               wsprintf((LPSTR)Temp,"Debug(0) returned %d",Code);
  211.               DisplayLine((LPSTR)Temp);
  212.               break;
  213.  
  214.             case MSG_WWW_HEAD:
  215.               InBufLen = 0;
  216.               DisplayLine("Enter URL (eg: 'www.marshallsoft.com/hello.htm') to view");
  217.               DisplayString("URL: ");
  218.               SetTheFocus();
  219.               break;
  220.            }
  221.          break;
  222.  
  223.     case WM_PAINT:
  224.       HideCaret(hMainWnd);
  225.       hDC = BeginPaint(hMainWnd, &ps);
  226.       SetMapMode(hDC,MM_ANISOTROPIC);
  227.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  228.       PaintMain(hDC,&ps);
  229.       EndPaint(hMainWnd,&ps);
  230.       SetCaretPos(PaintGetColPos(),PaintGetRowPos());
  231.       ShowCaret(hMainWnd);
  232.       break;
  233.  
  234.     case WM_DESTROY:
  235.       PostQuitMessage(0);
  236.       break;
  237.  
  238.     case WM_CHAR:
  239.       if(wParam==CR)
  240.         {/* do the CR */
  241.          DisplayChar((char)wParam);
  242.          /* user has completed input */
  243.          DestroyCaret();
  244.          DisplayChar(LF);
  245.          InBuffer[InBufLen] = '\0';
  246. #if 0
  247. wsprintf((LPSTR)Temp,"[%s]",(LPSTR)InBuffer); /*DEL*/
  248. DisplayLine((LPSTR)Temp); /*DEL*/
  249. #endif
  250.          StringCopy((LPSTR)WorkBuf,(LPSTR)InBuffer,MAX_BUF);
  251.          DisplayLine((LPSTR)WorkBuf);
  252.          /* extract the URL & URI */
  253.          Ptr = StringChar((LPSTR)WorkBuf,'/');
  254.  
  255. #if 0
  256. wsprintf((LPSTR)Temp,"(%s)",(LPSTR)Ptr); /*DEL*/
  257. DisplayLine((LPSTR)Temp); /*DEL*/
  258. #endif
  259.          if(Ptr)
  260.            {*Ptr = '\0';
  261.             StringCopy((LPSTR)URL,(LPSTR)WorkBuf,MAX_STR);
  262.             StringCopy((LPSTR)URI,(LPSTR)(Ptr+1),MAX_STR);
  263.            }
  264.          else
  265.            {/* assume "index.html" */
  266.             StringCopy((LPSTR)URL,(LPSTR)WorkBuf,MAX_STR);
  267.             StringCopy((LPSTR)URI,"index.html",MAX_STR);
  268.            }
  269.          /* excute command */
  270.          wsprintf((LPSTR)Temp,"URL: %s",(LPSTR)URL);
  271.          DisplayLine((LPSTR)Temp);
  272.          wsprintf((LPSTR)Temp,"URI: %s",(LPSTR)URI);
  273.          DisplayLine((LPSTR)Temp);
  274.          /* put up hour glass */
  275.          SetCursor(WaitCursor);
  276.          /* ask for host by name */
  277.          Code = wilAskHostByName((LPSTR) URL);
  278.          if(Code<0)
  279.            {DisplayError(Code, NULL);
  280.             break;
  281.            }
  282.          /* get host IP address */
  283.          HostAddr = wilGetHostAddr(0);
  284.          if(HostAddr==0)
  285.            {DisplayError(0, "Cannot get IP addess");
  286.             break;
  287.            }
  288.          wsprintf((LPSTR)Temp,"HostAddr = %lx\n", HostAddr);
  289.          DisplayLine((LPSTR)Temp);
  290.          /* create TCP socket */
  291.          Socket = wilTcpSocket();
  292.          if((int)Socket<0)
  293.            {DisplayError((int)Code, NULL);
  294.             break;
  295.            }
  296.          /* attempt to connect to remote host */
  297.          Code = wilConnect(Socket,HostAddr,WWW_PORT);
  298.          if(Code<0)
  299.            {DisplayError(Code, NULL);
  300.             break;
  301.            }
  302.          /* wait up to 30 seconds for connection */
  303.          if(wilIsConnected(Socket,30000))
  304.            {wsprintf((LPSTR)Temp,"Connected to %s", URL);
  305.             DisplayLine((LPSTR)Temp);
  306.            }
  307.          else
  308.            {/* couldn't connect within 30 seconds */
  309.             DisplayLine("Cannot CONNECT");
  310.             break;
  311.            }
  312.          /* send GET request */
  313.          wsprintf((LPSTR)Temp,"GET /%s HTTP/1.0\r\n",(LPSTR)URI);
  314.          DisplayString((LPSTR)Temp);
  315.          wilWriteLine(Socket,(LPSTR)Temp);
  316.          /* wait up to 20 seconds for response */
  317.          if(wilDataIsReady(Socket, TWENTY_SEC))
  318.            {/* read socket */
  319.             while(1)
  320.               {/* wait for next string */
  321.                if(!wilDataIsReady(Socket, ONE_SEC)) break;
  322.                /* read the string */
  323.                Code = wilReadString(Socket,(LPSTR)InBuffer,MAX_BUF);
  324.                if(Code<=0) break;
  325.                /* display for user */
  326.                wsprintf((LPSTR)Temp,"%s", (LPSTR)InBuffer);
  327.                DisplayString((LPSTR)Temp);
  328.               }
  329.             DisplayLine("");
  330.            }
  331.          else DisplayLine("No response from server.");
  332.          SetCursor(ArrowCursor);
  333.          wilCloseSocket(Socket);
  334.         }
  335.       else
  336.         {/* add char to input buffer */
  337.          Add2Buffer((char)wParam);
  338.         }
  339.       break;
  340.  
  341.     default:
  342.       return (DefWindowProc(hMainWnd, iMsg, wParam, lParam));
  343.    }
  344.  return 0;
  345.  
  346. } /* end MainWndProc */